package com.example.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.Domain.Inform;
import com.example.Domain.Manual;
import com.example.Domain.User;
import com.example.Util.JwtUtil;
import com.example.common.ServerResponse;
import com.example.service.Impl.ManualServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Manual")
public class ManualController {
    @Autowired
    private ManualServiceImpl manualservice;
    /*按类型查询手册*/
    @RequestMapping("/manualtype")
    public ServerResponse manualtype(HttpServletRequest request,String manual){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
     List<Manual> a=manualservice.manualtype(manual);
        return ServerResponse.createBySuccess("查询成功",a);
    }
    /*手册查询全部*/
    @RequestMapping("/manualall")
    public ServerResponse manualall(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
        List<Manual> a=manualservice.manualall();
            PageInfo<Manual> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功",pageInfo);
    }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*
    * 手册资源管理删除
    * */
    @RequestMapping("/manualdelete")
    public ServerResponse manualdelete(HttpServletRequest request,Integer id){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
        boolean a=manualservice.manualdelete(id);
        return ServerResponse.createBySuccess("删除成功",a);
    }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("删除失败");
        }

    }
    /*
     * 手册资源管理修改
     * */
    @RequestMapping("/manualupdate")
    public ServerResponse manualupdate( HttpServletRequest request, Manual manual){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
        Boolean a=manualservice.manualupdate(manual);
        return ServerResponse.createBySuccess("成功",a);
    }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }
    /*
     * 手册资源管理添加
     * */
    @RequestMapping("/manualinsert")
    public ServerResponse manualinsert(@RequestBody String Data, HttpServletRequest request, Manual manual){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject=JSONObject.parseObject(Data);
            String manualName=jsonObject.getString("manualName");//
            Date createTime=jsonObject.getDate("createTime");
            String describe=jsonObject.getString("describe");//
            String manualPdf=jsonObject.getString("manualPdf");//
            Integer maualPeople=jsonObject.getInteger("maualPeople");//
            Integer manualState=jsonObject.getInteger("manualState");//
            Integer userId=jsonObject.getInteger("userId");//
            String manualType=jsonObject.getString("manualType");//
            String manualImg=jsonObject.getString("manualImg");//
            userId=u.getId();
            manual.setUserId(userId);
            manual.setManualName(manualName);
            manual.setCreateTime(createTime);
            manual.setDescribe(describe);
            manual.setManualPdf(manualPdf);
            manual.setManualState(manualState);
            manual.setMaualPeople(maualPeople);
            manual.setManualType(manualType);
            manual.setManualImg(manualImg);

            System.out.println(manual+"++++++++++++++\n================================");
            boolean a=manualservice.manualinsert(manual);

            return ServerResponse.createBySuccess("成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }

    @RequestMapping("/ordertime")
    public ServerResponse ordertime(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Manual> a=manualservice.ordertime(pageNum, pageSize);
            PageInfo<Manual> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功:",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*
    * 模糊查询
    * */
    @RequestMapping("/likeselect")
    @ResponseBody
    public ServerResponse likeselect(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            String manual=jsonObject.getString("manualName");
            PageHelper.startPage(pageNum, pageSize);
            List<Manual> a=manualservice.likeselect(manual,pageNum, pageSize);
            PageInfo<Manual> pageInfo = new PageInfo<>(a);
            return ServerResponse.createBySuccess("查询成功:",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }

    /*数据回显*/
    @RequestMapping("/updateecho")
    public ServerResponse updateecho(HttpServletRequest request,Integer id){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            List<Manual> a=manualservice.updateecho(id);
            return ServerResponse.createBySuccess("成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }
}
